package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyDept;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyDeptMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.zltx.bean.SjApplyUpdateRecord;
import com.jy.adscm.zltx.bean.SjApprovalRecord;
import com.jy.adscm.zltx.bean.SjFile;
import com.jy.adscm.zltx.bean.SjFileTrain;
import com.jy.adscm.zltx.mapper.mysql.SjApplyUpdateRecordMapper;
import com.jy.adscm.zltx.mapper.mysql.SjApprovalRecordMapper;
import com.jy.adscm.zltx.mapper.mysql.SjFileMapper;
import com.jy.adscm.zltx.mapper.mysql.SjFileTrainMapper;
import com.jy.adscm.zltx.service.SjApprovalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-11-08
 */
@Service
public class SjApprovalRecordServiceImpl extends ServiceImpl<SjApprovalRecordMapper, SjApprovalRecord> implements SjApprovalRecordService {
    @Autowired
     SjApplyUpdateRecordMapper sjApplyUpdateRecordMapper;
    @Autowired
    SjFileMapper sjFileMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    SjFileTrainMapper sjFileTrainMapper;
    @Autowired
    JyDeptMapper jyDeptMapper;
    @Override
    public boolean updateSjApprovalRecord(SjApprovalRecord sjApprovalRecord) {
        //先进行修改 

        SjApplyUpdateRecord sjApplyUpdateRecord = sjApplyUpdateRecordMapper.selectById(sjApprovalRecord.getUpdateId());
        boolean result = super.updateById(sjApprovalRecord);
        Wrapper<SjApprovalRecord> wrapper = new EntityWrapper<>();
        wrapper.eq("updateId",sjApprovalRecord.getUpdateId());
        List<SjApprovalRecord> sjApprovalRecords = super.selectList(wrapper);
        int temp=0;
        for (SjApprovalRecord approvalRecord : sjApprovalRecords) {
         if (approvalRecord.getApprovalTime()==null){
             break;
         }
         if ("0".equals(approvalRecord.getIsApproval())){

                if (sjApplyUpdateRecord!=null){
                    sjApplyUpdateRecord.setApprovalResult("0");
                    sjApplyUpdateRecordMapper.updateById(sjApplyUpdateRecord);
                }
                break;
            }
            temp++;
        }
        //审核通过
        if (temp == sjApprovalRecords.size()){
            SjFile sjFile = sjFileMapper.selectById(sjApprovalRecord.getFileId());
            //修改申请记录
            sjApplyUpdateRecord.setApprovalResult("1");
            sjApplyUpdateRecord.setBeforeContent(sjFile.getInitialContent());
            sjApplyUpdateRecord.setAfterContent(sjApplyUpdateRecord.getInitialContent());
            sjApplyUpdateRecordMapper.updateById(sjApplyUpdateRecord);
            //生成培训记录
            if ( "1".equals( sjApplyUpdateRecord.getIsTrain())){
                SjFileTrain  sjFileTrain=new SjFileTrain();
                sjFileTrain.setFileId(sjApplyUpdateRecord.getFileId());
                sjFileTrain.setVersionCode(sjApplyUpdateRecord.getVersionCode());
                sjFileTrainMapper.insert(sjFileTrain);
            }
           //修改基本信息
            sjFile.setUpdateTimes(sjFile.getUpdateTimes()+1);
            sjFile.setLastUpdateTime(new Date());
            sjFile.setIsApproval("0");
            sjFile.setInitialContent(sjApplyUpdateRecord.getInitialContent());
            sjFile.setFileCode(sjApplyUpdateRecord.getFileCode());
            sjFile.setVersionCode(sjApplyUpdateRecord.getVersionCode());
            sjFileMapper.updateById(sjFile);


        }


        return result;
    }

    @Override
    public List<SjApprovalRecord> selectByUpdateId(Integer updateId) throws AdscmSystemException {
           Wrapper<SjApprovalRecord> wrapper = new EntityWrapper<>();
           wrapper.eq("updateId",updateId);
        List<SjApprovalRecord> sjApprovalRecords = super.selectList(wrapper);
        for (SjApprovalRecord sjApprovalRecord : sjApprovalRecords) {
             if (sjApprovalRecord.getApprovaler()!=null && !"".equals(sjApprovalRecord.getApprovaler())){
                 JyUser jyUser = jyUserMapper.selectById(sjApprovalRecord.getApprovaler());
                 sjApprovalRecord.setApprovalerName(jyUser.getName());
             }
             if (sjApprovalRecord.getDeptId()!=null&&!"".equals(sjApprovalRecord.getDeptId())){
                 JyDept jyDept = jyDeptMapper.selectById(sjApprovalRecord.getDeptId());
                 sjApprovalRecord.setDeptName(jyDept.getDeptName());
             }
        }
        return sjApprovalRecords ;
    }
}
